package yc.web.servlets;

import yc.bean.CartItem;
import yc.bean.Resorder;
import yc.bean.User;
import yc.dao.DBHelper;

import java.sql.*;
import java.util.Set;

public class OrederBiz {
    /*以事务处理方式来添加订单*/
    public int order(Resorder resorder, Set<CartItem>cartItems, User user) throws Exception {
        if (cartItems==null|| cartItems.size()<=0){
            throw new RuntimeException("订单为空");
        }
        int result=0;
        DBHelper db=new DBHelper();
        Integer roid=0;
        //事务处理
        Connection con= db.getConnection();
        try {
            con.setAutoCommit(false);//关闭隐式事务提交
            String sql = "insert into resorder( userid,address,tel,ordertime,deliverytime,ps,status)values(?,?,?,now(),?,?,0)";
            PreparedStatement pstmt = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);//RETURN_GENERATED_KEYS代表获取自动生成的id
            pstmt.setString(1, user.getUserid() + "");
            pstmt.setString(2, resorder.getAdress());
            pstmt.setString(3, resorder.getTel());
            pstmt.setString(4, resorder.getDeliverytime());
            pstmt.setString(5, resorder.getPs());
            pstmt.executeUpdate();
            //取自动生成的id
            ResultSet rs = pstmt.getGeneratedKeys();
            if (rs.next()) {
                roid = rs.getInt(1);
            }
//循环所有的订单项，添加到数据表resorderitem
            String sql2 = "insert into resorderitem(roid,fid,dealprice,num)values(?,?,?,?)";
            for (CartItem ci : cartItems) {
                pstmt = con.prepareStatement(sql2);
                pstmt.setString(1, roid + "");
                pstmt.setString(2, ci.getFood().getFid() + "");
                pstmt.setString(3, ci.getFood().getRealprice() + "");
                pstmt.setString(4, ci.getNum() + "");
                pstmt.executeUpdate();
            }
            con.commit();//事务提交
        }catch (Exception ex){
            ex.printStackTrace();
            if (con!=null){
                con.rollback();
            }throw ex;
        }finally {
            if (con!=null){
                con.close();
            }
        }
        return result;
    }
}
